<template>
  <div class="page-container">
    <div v-if="loading" class="loading">加载中...</div>
    <div v-else-if="error" class="error">{{ error }}</div>
    <div class="page-content" v-else>
      <div class="content-left">

        <div class="article-viewer"> <!-- 添加 v-else -->
          <!-- 文章标题 -->
          <div class="viewer-header">
            <h1>{{ article.title }}</h1>

            <p class="article-meta">
              <span>作者：{{ article.author }}</span>
              <span>分类：{{ article.category_name }}</span>
              <span v-if="article.publish_date">发布时间：{{ article.publish_date }}</span>
              <span v-else>创建时间：{{ article.created_date }}</span>
              <span>当前状态：{{ article_status[article.status] }}</span>
            </p>
            <div class="action-buttons" v-if="article">
              <button class="btn-edit" @click="$router.push({ name: 'edit-article', params: { id: article.id } })">
                编辑
              </button>
              <button class="btn-delete" @click="handleDelete">
                删除
              </button>
              <button
                  v-if="article.status === 'draft'"
                  class="btn-publish"
                  @click="handlePublish">
                发布
              </button>
            </div>
          </div>

          <!-- 封面图 -->
          <div
              class="cover-image"
              v-if="article.image"
              :style="{ backgroundImage: `url(${article.image})` }">
          </div>
          <div class="cover-images" v-if="article.images">
            <img :src="item.image" v-for="item in article.images" :key="item.id"/>
          </div>

          <!-- 标签 -->
          <div class="article-tags">
      <span class="tag" v-for="tag in article.tags" :key="tag.id">
        {{ tag.name }}
      </span>
          </div>

          <!-- 摘要 -->
          <div class="article-summary">
            <p v-html="article.excerpt"></p>
          </div>

          <!-- 正文内容 -->
          <div class="article-content">
            <div v-html="article.content"></div>
          </div>
        </div>
      </div>
      <div class="content-right">
        <!-- 目录容器 -->
        <div v-if="article.toc_html" class="toc-container">
          <h3 class="toc-title">目录</h3>
          <div class="toc-content" v-html="article.toc_html"></div>
        </div>
      </div>

    </div>
  </div>

</template>

<script setup>
const route = useRoute()
const router = useRouter()
const article = ref(null)
const loading = ref(true)
const error = ref(null)
//获取状态
const article_status = {
  draft: '草稿',
  published: '已发布',
  deleted: '已删除',
  pending: '待审核'
}

// 获取文章
const fetchArticle = async () => {
  try {
    if (!route.params.id) {
      error.value = '无效的文章ID'
      return
    }
    const response = await axios.get(`/api/cms/articles/${route.params.id}`)
    article.value = response.data
    await router.push({ name: 'show-article', params: { id: route.params.id }, query: { title: response.data.title } })
  } catch (err) {
    error.value = err.response?.data?.message || '获取文章失败，请稍后重试'
  } finally {
    loading.value = false
  }
}
// 删除文章
const handleDelete = async () => {
  if (!confirm('确定要删除这篇文章吗？')) return
  try {
    await axios.delete(`/api/cms/articles/${article.value.id}/delete_article/`, {requiresAuth: true})
    await router.push({'name': 'list-articles'})
  } catch (error) {
    alert('删除失败')
  }
}

// 发布文章
const handlePublish = async () => {
  try {
    const {data} = await axios.put(`/api/cms/articles/${article.value.id}/publish_article/`, {}, {requiresAuth: true})
    article.value.status = data.status
    router.replace({
      path: `/admin-articles/show/${route.params.id}`,
      query: {t: Date.now()} // 添加时间戳避免缓存
    })
    alert('提交发布成功，请耐心等待管理员审核')
  } catch (error) {
    alert('发布失败')
  }
}
// 生命周期钩子
onMounted(() => {
  fetchArticle()
})


</script>

<style scoped>
.page-content {
  margin-top: 20px;
}

.content-right {
  margin-top: 0;
}

.article-viewer {
  margin: 0 auto;
  padding: 20px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  font-family: Arial, sans-serif;
  line-height: 1.6;
}

.viewer-header {
  text-align: center;
}

.viewer-header h1 {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  margin-bottom: 10px;
}

.article-meta {
  font-size: 14px;
  color: #888;
}

.article-meta span {
  margin-right: 10px;
}

.cover-image {
  width: 100%;
  height: 200px;
  background-size: cover;
  background-position: center;
  border-radius: 8px;
  margin: 20px 0;
}

.cover-images {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(24%, 1fr));
  grid-gap: 15px;
  margin: 20px 0;

}

.cover-images img {
  width: 100%;
  height: auto;
  max-height: 250px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .1);
}


.article-tags {
  margin-bottom: 20px;
}

.tag {
  background: rgba(0, 123, 255, .08);
  color: #007bff;
  padding: 6px 12px;
  border-radius: 20px;
  transition: all 0.2s;
  cursor: pointer;
}

.tag:hover {
  background: #007bff;
  color: white;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 123, 255, .2);
}


.article-summary {
  margin-bottom: 30px;
  padding: 10px 18px;
  border-left: 3px solid #007bff;
  background: #f9f9f9;
  font-style: italic;
}

.article-content {
  line-height: 1.8;
  font-size: 16px;
  color: #444;
}

.article-content :deep(h2) {
  font-size: 1.6rem;
  margin: 2rem 0 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid #eee;
}

.article-content :deep(img) {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin: 1.5rem 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .1);
}


.article-content p {
  margin-bottom: 10px;
}


.action-buttons {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: flex-end;
  margin: 0;
  padding: 0;
}

.action-buttons button {
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid transparent;
}

.btn-edit {
  background: #007bff;
  color: white;
}

.btn-edit:hover {
  background: #0056b3;
}

.btn-delete {
  background: #dc3545;
  color: white;
}

.btn-delete:hover {
  background: #bb2d3b;
}

.btn-publish {
  background: #28a745;
  color: white;
}

.btn-publish:hover {
  background: #218838;
}

/* 新增/修改的目录样式 */
.toc-container {
  position: sticky;
  max-width: 280px;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

.toc-title {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 15px;
  position: relative;
  padding-left: 12px;
}

.toc-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 16px;
  background: linear-gradient(to bottom, #007bff, #3da5ff);
  border-radius: 2px;
}

.toc-content {
  font-size: 14px;
  line-height: 1.6;
}

.toc-content :deep(ul) {
  list-style: none;
  padding-left: 0;
  margin: 0;
}

.toc-content :deep(li) {
  margin: 6px 0;
  padding-left: 1em;
  position: relative;
}

.toc-content :deep(a) {
  color: #4a5568;
  text-decoration: none;
  transition: color 0.2s;
}

.toc-content :deep(a:hover) {
  color: #007bff;
}

/* 不同层级样式 */
.toc-content :deep(.toc-h2) {
  font-weight: 500;
  margin-left: 0;
}

.toc-content :deep(.toc-h3) {
  margin-left: 1em;
  font-weight: 400;
}

.toc-content :deep(.toc-h4) {
  margin-left: 2em;
  font-size: 13px;
  color: #718096;
}

.toc-content :deep(.active) {
  color: #007bff !important;
  font-weight: 500;
}

.loading {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 50vh;
  font-size: 1.2rem;
}

.loading::after {
  content: "";
  display: inline-block;
  width: 1.2em;
  height: 1.2em;
  border: 2px solid #007bff;
  border-radius: 50%;
  border-top-color: transparent;
  margin-left: 10px;
  animation: spin 0.8s linear infinite;
}

.error {
  color: #dc3545;
  padding: 40px;
  text-align: center;
  border: 1px solid #ffcccc;
  margin: 20px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 768px) {
  .article-meta {
    font-size: 12px;
    padding: 0;

  }

  .article-meta span {
    margin-right: 5px;
  }

  .content-right {
    margin-top: 20px;
  }

  .toc-container {
    max-width: none;
  }
}

</style>